package com.example.filesave;

import android.content.SharedPreferences;
import android.icu.text.IDNA;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.filesave.util.BaseActivity;
import com.example.filesave.util.L;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Login extends BaseActivity implements View.OnClickListener{

    EditText user;
    EditText pwd;
    CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Button btn = (Button) findViewById(R.id.btnlogin);
        user = (EditText) findViewById(R.id.user);
        pwd = (EditText) findViewById(R.id.pwd);
        checkBox = (CheckBox)findViewById(R.id.checkbox);

        btn.setOnClickListener(this);
        // getFilesDir()获取文件存储路径
//        File file = new File(getFilesDir(), "info.txt");
        //  getCacheDir() 获取缓存路径 ，设备存储满时会清除
//        File file = new File(getCacheDir(), "info.txt");
//        Environment.getExternalStorageDirectory() 外部存储地址
      /*  File file = new File(Environment.getExternalStorageDirectory(),"info.txt");

        if(file.exists()){
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                // 把字节流转为字符流
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
                // 每次读一行
                String text = bufferedReader.readLine();
                // 打断
                String[] s = text.split(":");
                // 设置回文本框
                user.setText(s[0]);
                pwd.setText(s[1]);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/

        // SharedPreferences取
        SharedPreferences info = getSharedPreferences("info", MODE_APPEND);
        // 获取编辑器
        user.setText(info.getString("user",""));
        pwd.setText(info.getString("pwd",""));



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnlogin:
                L.d("点击了登录");
                String name = user.getText().toString();
                String password = pwd.getText().toString();
                String s = name + ":"+ password;
                // 点击登陆 如果选择记住密码
                if (checkBox.isChecked()){
                    // 确定文件名和路径
                    L.d(getFilesDir().toString());
//                    File file = new File(getFilesDir(),"info.txt");
//                    File file = new File(getCacheDir(),"info.txt");

                  /*  File file = null;
                    // MEDIA_REMOVED;
                    //sd卡中的状态
                    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                        file = new File(Environment.getExternalStorageDirectory(),"info.txt");
                    }else {
                        Toast.makeText(this,"SD卡不可用哦！！！",Toast.LENGTH_LONG).show();
                        L.d("SD卡不可用哦！！！");
                        return;
                    }

                    try {
                        FileOutputStream fos = new FileOutputStream(file);
                        fos.write(s.getBytes());
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }*/

                    SharedPreferences info = getSharedPreferences("info", MODE_APPEND);
                    // 获取编辑器
                    SharedPreferences.Editor editor = info.edit();
                    editor.putString("user",name);
                    editor.putString("pwd",password);
                    // 提交
                    editor.commit();
                    Toast.makeText(this,"记住密码登录成功",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(this,"登录成功",Toast.LENGTH_LONG).show();
                }
                break;
            default:
                break;
        }
        L.d(R.id.btnlogin+"");
    }
}
